$(function () {
    //变量抽取
    var clientDevelop_dataGrid = $("#clientDevelop_dataGrid");
    var clientDevelop_dialog = $("#clientDevelop_dialog");
    var clientDevelop_form = $("#clientDevelop_form");
    var client_dataGrid = $("#client_dataGrid");
    var client_dialog = $("#client_dialog");
    var remark_dialog = $("#remark_dialog");
    var remark_form = $("#remark_form");

    //页面数据列表
    clientDevelop_dataGrid.datagrid({
        url: "/clientDevelop/query.do",
        fitColumns: true,//列自适应
        fit: true,
        align: 'center',
        queryParams:{
            state:'0'
        },
        columns: [[
            {field: 'id', title: '编号', width: 100, align: 'center'},
            {
                field: 'result', title: '执行结果', width: 100, align: 'center',formatter: function (value, row, index) {
                    if (row.result == 0) {
                        return "";
                    } else if(row.result == 1){
                        return "<font color='red'>差</font>";
                    }else if(row.result == 2){
                        return "<font color='yellow'>中</font>";
                    }else if(row.result == 3){
                        return "<font color='green'>优</font>";
                    }
                }
            },
            {
                field: 'operateDate', title: '计划时间', width: 100, align: 'center'
            },
            {
                field: 'createEmp', title: '创建人', width: 100, align: 'center'
            },
            {field: 'clientName', title: '客户', width: 100, align: 'center'},
            {
                field: 'project', title: '计划主题', width: 100, align: 'center'
            },
            {
                field: 'method', title: '实施方式', width: 100, align: 'center', formatter: function (value) {
                    return value ? value.name : "";
                }
            },
            {
                field: 'particular', title: '详情', width: 100, align: 'center'
            },
        {
                field: 'remark', title: '备注', width: 100, align: 'center'
            }
        ]],
        toolbar: "#tb",//工具
        pagination: true,//分页
        rownumbers: true,//行号
        singleSelect: true//只允许选择一行
    });

    //查询按钮数据列表
    client_dataGrid.datagrid({
        url: "/client/query.do",
        fitColumns: true,//列自适应
        fit: true,
        queryParams: {
            state: 0
        },
        align: 'center',
        columns: [[
            {
                field: 'createEmp', title: '负责人', width: 100
            },
            {
                field: 'clientName', title: '客户名称', width: 100
            },
            {
                field: 'gender', title: '性别', width: 100, formatter: function (value) {
                    return value ? "男" : "女";
                }
            },
            {field: 'tel', title: '电话', width: 100, align: 'center'},
            {field: 'email', title: '邮箱', width: 100, align: 'center'},
            {field: 'clientAddress', title: '客户地址', width: 100, align: 'center'},
            {field: 'idCard', title: '身份证', width: 100, align: 'center'}
        ]],
        pagination: true,//分页
        rownumbers: true,//行号
        singleSelect: true//只允许选择一行
    });

    //列表弹窗
    clientDevelop_dialog.dialog({
        width: 500,
        height: 320,
        buttons: "#btns",
        closed: true,
        onClose: function () {
            clientDevelop_form.form("clear")
        },
        resizeable:true
    });

    //查询弹窗
    client_dialog.dialog({
        width: 700,
        height: 370,
        buttons: "#clientBtns",
        closed: true
    });

    //标记弹窗
    remark_dialog.dialog({
        width: 300,
        height: 220,
        buttons: "#remarkBtns",
        closed: true,
        onClose: function () {
            clientDevelop_form.form("clear")
        }
    });

    //方法抽成对象统一管理
    var methodObj = {

        //添加按钮
        add: function () {
            clientDevelop_dialog.dialog("open");
            clientDevelop_dialog.dialog("setTitle", "新增计划");
        },

        //编辑按钮
        edit: function () {
            var row = clientDevelop_dataGrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('提示', '至少选择一条需要修改的计划', 'info');
            } else {
                row["method.id"] = row.method.id;
                clientDevelop_form.form("load", row);
                clientDevelop_dialog.dialog("setTitle", "修改计划");
                clientDevelop_dialog.dialog("open");
            }
        },

        //删除计划
        delDevelop:function () {
            var row = clientDevelop_dataGrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('提示', '至少选择一条要删除的计划数据', 'info');
            } else {
                $.messager.confirm("提示", "确定要删除该条计划数据？", function (r) {
                    if (r) {
                        $.get('/clientDevelop/deleteRecordById.do', {id: row.id}, function (data) {
                            if (data.success) {
                                $.messager.alert('提示', '删除成功', 'info', function () {
                                    clientDevelop_dialog.dialog("close");
                                    clientDevelop_dataGrid.datagrid("reload");
                                })
                            } else {
                                $.messager.alert("提示", "删除失败", 'error');
                            }
                        });
                    }
                })
            }
        },

        //标记结果
        remark:function () {
            var row = clientDevelop_dataGrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('提示', '至少选择一条需要修改的计划', 'info');
            } else {
                $("#idEcho").textbox("setValue",row.id);
                $("#projectEcho").textbox("setValue",row.project);
                $("#resultId").combobox("setValue",row.result);
                remark_dialog.dialog("setTitle", "标记执行结果");
                remark_dialog.dialog("open");
            }
        },

        //提交按钮
        commit:function () {
            remark_form.form("submit",{
                url: "/clientDevelop/changeResult.do",
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data) {
                        $.messager.alert('提示', '保存成功', 'info', function () {
                            remark_dialog.dialog("close");
                            clientDevelop_dataGrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert('提示', '保存失败', 'info');
                    }
                }
            });
        },

        //取消按钮
        remarkCancel: function () {
            remark_dialog.dialog("close")
        },

        //保存按钮
        save: function () {
            clientDevelop_form.form("submit", {
                url: "/clientDevelop/saveOrUpdate.do",
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data) {
                        $.messager.alert('提示', '保存成功', 'info', function () {

                            clientDevelop_dialog.dialog("close");
                            clientDevelop_dataGrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert('提示', '保存失败', 'info');
                    }
                }
            });
        },

        //取消按钮
        cancel: function () {
            clientDevelop_dialog.dialog("close")
        },

        //刷新按钮
        reload: function () {
            //清空查询条件
            $("#beginDate").textbox("setValue",'');
            $("#endDate").textbox("setValue",'');
            $("#clientName").textbox("setValue",'');
            $("#createEmp").textbox("setValue",'');
            $("#project").textbox("setValue",'');
            $(".develop_combobox").textbox("setValue",'');
            clientDevelop_dataGrid.datagrid("load",{
                state: 0
            });
        },

        //查询按钮
        searchMethod:function () {

            //获取输入框的内容
            var beginDate = $("[name=beginDate]").val();
            var endDate = $("[name=endDate]").val();
            var clientName = $("[name=clientName]").val();
            var createEmp = $("[name=createEmp]").val();
            var project = $("[name=project]").val();
            var methodId = $(".develop_combobox").combobox('getValue');

            clientDevelop_dataGrid.datagrid('load', {
                beginDate: beginDate,
                endDate: endDate,
                clientName: clientName,
                createEmp: createEmp,
                project: project,
                methodId: methodId,
                state: 0
            });
        },

        //查询客户对象
        searchClient:function () {
            client_dialog.dialog("setTitle", "请选择潜在客户");
            client_dialog.dialog("open");
        },

        //查询客户对象确认按钮
        ensure:function () {
            var row = client_dataGrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('提示', '至少选择一位潜在客户', 'info');
            } else {
                $("#clientId").val(row.id);
                $(".clientName_btn").textbox("setValue", row.clientName);
                client_dialog.dialog("close");
            }
        }
    };

    //按钮统一绑定事件
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd]();
    });

});


